﻿Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
'This is a Parent Class for Consequence, Option and Goal Cards
Public Class CardRule
    'Card ID Retrieved from database
    Public cardID As String
    'System ID for the Played Game
    Public systemID As String

    'Card Attribute List. Contains list of object type of CardAttributes

    Public cardAttribsEnterPlay As ArrayList = New ArrayList()
    Public cardAttribsCheckRemain As ArrayList = New ArrayList()
    Public cardAttribsAdd As ArrayList = New ArrayList()
    Public cardAttribsRemove As ArrayList = New ArrayList()

    'Card Specific
    'Card Attribute List. Contains list of object type of CardAttributes

    Public cardAttribsRequiredToPlay As ArrayList = New ArrayList()
    Public cardAttribsRemainInPlay As ArrayList = New ArrayList()
    Public cardAttribsLeavePlay As ArrayList = New ArrayList()

    Public cardAttributeTotal As ArrayList = New ArrayList()

    'Constructor for CardRule
    Public Sub New(cardIDPassed, systemIDpassed)
        cardID = cardIDPassed
        systemID = systemIDpassed
        'Card initialization Array
    End Sub

    ''Modify Status of the card in the game
    Public Sub modMatchCardGame(ByVal cardID As String, matchID As String, inCommons As Integer, inDiscard As Integer, isDrawn As Integer)
        'Connection Variables
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCmd As SqlCommand = New SqlCommand
        If isDrawn = 1 Then
            SqlCmd.CommandText = "INSERT INTO Match_GameCard (MatchID, CardID, IsDrawn, InCommons, InDiscard) VALUES ('" & cardID & "', '" & _
              matchID & "', '" & isDrawn & "', '" & inCommons & "', '" & inDiscard & "')"
        End If
        connectLink.doUpdate2(SqlCmd.CommandText)
    End Sub

    'Get Card, then create appropriate card, such as consequence and other.
    Public Sub playCard(ByVal cardID As String, systemID As String, matchID As String, action As String, ajaxGEorig As ajaxGameEngine)
        'Change Table
        Dim ajaxGE As ajaxGameEngine = ajaxGEorig
        modMatchCardGame(cardID, matchID, 0, 0, 1)
        'Get modifiers that will make an influence on the Game Board.
        Dim actionItem As RuleActions
        'Make an impact on the system
        fillAttirubtes()
        actionItem = New RuleActions(cardAttributeTotal, action)
        actionItem.makeAction(ajaxGE, Me, systemID)
    End Sub

    'Fill Attributes for all cases
    Private Sub fillAttirubtes()
        'Get modifiers that will make an influence on the Game Board.
        fillCardAttributeLeavePlay(cardID, systemID)
        fillCardAttributeRemainInPlay(cardID, systemID)
        fillCardAttributeRequiredToPlay(cardID, systemID)
        fillCardAttributeEnterPlay(cardID, systemID)
        'fillCardAttributeAdd(cardID, systemID)
        'fillCardAttributeCheckRemain(cardID, systemID)

        cardAttributeTotal.Add(cardAttribsLeavePlay)
        cardAttributeTotal.Add(cardAttribsRemainInPlay)
        cardAttributeTotal.Add(cardAttribsRequiredToPlay)
        cardAttributeTotal.Add(cardAttribsAdd)
        cardAttributeTotal.Add(cardAttribsCheckRemain)
        cardAttributeTotal.Add(cardAttribsEnterPlay)
    End Sub

    'Receive and fill attributes for the card and values for it
    Friend Sub fillCardAttributes(cardID As String, systemID As String, playAttribute As String)
        'Database Variables
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCmd As SqlCommand = New SqlCommand()
        Dim connReader As SqlDataReader
        Dim attributeID As String
        Dim value As Integer
        SqlCmd.CommandText = "SELECT CardID, PlayAttributeID, AttributeID, Value FROM CardPlayAttribute_SystemAttribute WHERE SystemID = '" & _
            systemID & "' AND PlayAttributeID = '" & playAttribute & "' AND CardID ='" & cardID & "'"
        'Run Query
        Try
            'Return Reader 
            connReader = connectLink.getDataReader(SqlCmd.CommandText)

            'Check if Data Reader has rows
            If connReader.HasRows Then
                'Read data from reader
                While connReader.Read()
                    cardID = connReader("CardID").ToString
                    attributeID = connReader("AttributeID").ToString
                    value = connReader("value")
                    Dim Attribute = New CardAttributes()
                    Attribute.cardAttribute = attributeID
                    Attribute.cardAttributeValue = value
                    If playAttribute = 1 Then
                        cardAttribsRequiredToPlay.Add(Attribute)
                    End If
                    If playAttribute = 2 Then
                        cardAttribsEnterPlay.Add(Attribute)
                    End If
                    If playAttribute = 3 Then
                        cardAttribsLeavePlay.Add(Attribute)
                    End If
                    If playAttribute = 4 Then
                        cardAttribsRemainInPlay.Add(Attribute)
                    End If
                    'Create array for Add Remain
                    If playAttribute = 2 Then
                        cardAttribsAdd.Add(Attribute)
                    End If
                    'Create array for Check-in Remain
                    'If playAttribute = 1 Then
                    '    cardAttribsCheckRemain.Add(Attribute)
                    'End If
                    ''Create array for Remove Remain
                    'If playAttribute = 3 Then
                    '    cardAttribsRemove.Add(Attribute)
                    'End If
                End While
                connReader.Close()
            End If
        Catch ex As Exception
            MsgBox("Error Retrieving Data. Fill CardAttributes")
        End Try
    End Sub

    'Fill the card attributes for Enter Play
    Friend Sub fillCardAttributeEnterPlay(ByVal cardID As String, systemID As String)
        fillCardAttributes(cardID, systemID, 2)
    End Sub

    ''Fill the card attributes for Enter Play
    'Friend Sub fillCardAttributeRemove(ByVal cardID As String, systemID As String)
    '    fillCardAttributes(cardID, systemID, 3)
    'End Sub

    ''Fill the card Check In Play
    'Friend Sub fillCardAttributeAdd(ByVal cardID As String, systemID As String)
    '    fillCardAttributes(cardID, systemID, 2)
    'End Sub

    'Fill the card Remain In Play
    'Friend Sub fillCardAttributeCheckRemain(ByVal cardID As String, systemID As String)
    '    fillCardAttributes(cardID, systemID, 4)
    'End Sub

    'Fill the card Remain In Play
    Friend Sub fillCardAttributeLeavePlay(ByVal cardID As String, systemID As String)
        fillCardAttributes(cardID, systemID, 3)
    End Sub

    'Fill the card Remain In Play
    Friend Sub fillCardAttributeRemainInPlay(ByVal cardID As String, systemID As String)
        fillCardAttributes(cardID, systemID, 4)
    End Sub


    'Fill the card Remain In Play
    Friend Sub fillCardAttributeRequiredToPlay(ByVal cardID As String, systemID As String)
        fillCardAttributes(cardID, systemID, 1)
    End Sub

    Friend Function printAttribValues()
        Dim s As String = ""
        Dim index As Integer
        'Print out from array ADD
        For index = 0 To cardAttribsAdd.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsAdd(index)
            s = s & "Card Attribs Add: " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value Add: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        'Print out array Remove
        For index = 0 To cardAttribsRemove.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRemove(index)
            s = s & "Card Attribs Remove: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value Remove: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        For index = 0 To cardAttribsCheckRemain.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsCheckRemain(index)
            s = s & "Card Attribs CheckRemain: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value CheckRemain: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        For index = 0 To cardAttribsEnterPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsEnterPlay(index)
            s = s & "Card Attribs EnterPlay: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value EnterPlay: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        For index = 0 To cardAttribsLeavePlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsLeavePlay(index)
            s = s & "Card Attribs LeavePlay: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value LeavePlay: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        For index = 0 To cardAttribsRemainInPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRemainInPlay(index)
            s = s & "Card Attribs RemainInPlay: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value RemaininpLay: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        For index = 0 To cardAttribsRequiredToPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRequiredToPlay(index)
            s = s & "Card Attribs RequiredToPlay: <br> " & attrib.cardAttribute.ToString & "<br/>" & _
                "Card Value RequiredToPlay: " & attrib.cardAttributeValue.ToString & "<br/>"
        Next
        Return s
    End Function

    'This function is changing System Attribute Values (SAV)
    Friend Sub changeAttribValues(ByRef ajaxGEorig As ajaxGameEngine)
        Dim index As Integer
        Dim ajaxGE As ajaxGameEngine = ajaxGEorig
        'Print out from array ADD
        For index = 0 To cardAttribsAdd.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsAdd(index)
            ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsRemove.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRemove(index)
            ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsCheckRemain.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsCheckRemain(index)
            ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsEnterPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsEnterPlay(index)
            ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsLeavePlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsLeavePlay(index)
            ajaxGE.modifySysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsRemainInPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRemainInPlay(index)
            'Will check attributes and leave card in play if attribs satisfies the conditions
            ajaxGE.checkSysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next
        'Apply elements from array to the system attribute values
        For index = 0 To cardAttribsRequiredToPlay.Count - 1
            Dim attrib As CardAttributes = New CardAttributes()
            attrib = cardAttribsRequiredToPlay(index)
            'Check Item and see if the system attributes have enough value in order to play
            ajaxGE.checkSysAtt(systemID, attrib.cardAttribute, attrib.cardAttributeValue)
        Next

    End Sub
    
End Class